package com.example.xueshen.common.controller;

import com.alibaba.druid.util.StringUtils;
import com.example.xueshen.common.entity.SystemUser;
import com.example.xueshen.common.service.SystemUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class userController {
    @Resource
    private SystemUserService systemUserService;
    @PostMapping("/login")
    public String login(SystemUser systemUser, HttpServletRequest request) {
        System.out.println("systemUser = " + systemUser);
        if (!StringUtils.isEmpty(systemUser.getUserName()) && !StringUtils.isEmpty(systemUser.getPassword())) {
            SystemUser systemUser1 =  systemUserService.selectOneByNameAndPassword(systemUser);
            if (systemUser1 != null) {
                request.getSession().setAttribute("loginUserName", systemUser.getUserName());
                return "redirect:/main.html";
            }
        }
        request.setAttribute("login_msg","用户名或密码错误");
        return "login";
    }
    @GetMapping("/loginOut")
    public String loginOut(HttpSession session){
        session.invalidate();
        return "redirect:/";
    }
}
